var helper = require('../../libs/helper'),
    logger = require('../../libs/logger').logger,
    
    Note = require('../../models/Note');

var getPageNotes = function(fields){
    return function(req, res){
        logger.debug('get Notes listing', req.query, req.body);
        var storeParams = helper.getStoreParams(req);

        Note.findAllOfPage(storeParams, fields, function(err, result){
            if(err) {
                return res.send({
                    success: false,
                    message: err.message || err.toString()
                });
            }
            result.success = true;
            res.json(result);
        });
    };
};

var addNote = function(req, res){
    delete req.body.ID;
    var note = new Note(req.body);
    note.save(function(err, result){
        if (err) {
            return res.send({
                success: false,
                message: err.message || err.toString()
            });
        }
        res.send({
            success: true,
            data: result
        });
    });
};

var updateNote = function(req, res){ //update
    var note = new Note(req.body);
    note.ID = req.params.id;

    note.save(function(err, result){
        if (err) {
            return res.send({
                success: false,
                message: err.message || err.toString()
            });
        }
        res.send({
            success: true
        });
    });
};

var getNote = function(req, res) { 
    Note.get(req.params.id, function(err, result){
        if (err) {
            return res.send({
                success: false,
                message: err.message || err.toString()
            });
        }
        res.json({
            success: true,
            data: result
        });
    });
};

var deleteNote = function(req, res) {
    Note.delete(req.params.id, function(err, result){
        if (err) {
            return res.send({
                success: false,
                message: err.message || err.toString()
            });
        }
        res.json({
            success: true
        });
    });
};

module.exports = {
    getPageNotes: getPageNotes,
    addNote: addNote,
    updateNote: updateNote,
    getNote: getNote,
    deleteNote: deleteNote
};